package com.test.liuhb.test.Util;

import android.annotation.TargetApi;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.test.liuhb.test.BaseActivity;
import com.test.liuhb.test.R;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * Created by xu.ch on 2016/5/18.
 */
public class PrinterConnectActivity extends BaseActivity implements AdapterView.OnItemClickListener, View.OnClickListener {

    @Bind(R.id.m8_PrinterConn_List)
    ListView listView;
    @Bind(R.id.m8_PrintConn_openBlueTooth)
    Button vOpenBlueTooth;
    @Bind(R.id.m8_PrintConn_GetDevices)
    Button vGetBondDevices;


    List<BlueToothDevice> mList;
    BlueToothDevicesAdapter mAdapter;
    BluetoothAdapter bluetoothAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.m8_printer_connect);
        ButterKnife.bind(this);
        init();
        listView.setOnItemClickListener(this);
    }

    @Override
    public void next(View view) {

    }

    @Override
    public void pre(View view) {

    }

    private void init() {
        mList = new ArrayList<>();
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (bluetoothAdapter.isEnabled()) {
            System.out.println("蓝牙打开");
            vOpenBlueTooth.setText("关闭蓝牙");
        }
        getBlueToothDevices();

    }


    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    private void getBlueToothDevices() {
        Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();
        if (null == devices) {

            return;
        }
        if (devices.size() > 0) {
            for (Iterator<BluetoothDevice> it = devices.iterator(); it.hasNext(); ) {
                BluetoothDevice device = it.next();
                BlueToothDevice obj = new BlueToothDevice();
                obj.setName(device.getName());
                obj.setAddress(device.getAddress());
                mList.add(obj);
            }

            mAdapter = new BlueToothDevicesAdapter(this, mList);
            listView.setAdapter(mAdapter);
        }
    }

    private void openOrCloseBlueTooth() {
        if (bluetoothAdapter != null) {
            if (!bluetoothAdapter.isEnabled()) {
                Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                //请求打开蓝牙
                startActivity(intent);
                vGetBondDevices.setEnabled(true);
                vOpenBlueTooth.setText("关闭蓝牙");
            } else {
                bluetoothAdapter.disable();
                vOpenBlueTooth.setText("打开蓝牙");
            }
        } else {

        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String address = mList.get(position).address;
        Intent intent = new Intent();
        intent.putExtra("device_address", address);
        setResult(1, intent);
        this.finish();
    }


    //---------------------------全局事件-------------------------------------------//
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.m8_PrintConn_openBlueTooth:
                openOrCloseBlueTooth();
                break;
            case R.id.m8_PrintConn_GetDevices:
                if (!bluetoothAdapter.isEnabled()) {

                    return;
                }
                mList.clear();
                getBlueToothDevices();
                break;
        }
    }

}
